const api = require('../../../ws_micro_shop/utils/api.js');
const app = getApp();

let is_no_more = false;
let is_loading = false;
let p = 2;

Page({
  data: {
    status: -1,
    cash_list: [],
    show_no_data_tip: false,
    theme: app.themeData,
    abnorData: {
      imgSrc: app.imgUrl + 'abnor-19.png',
      text: '暂无明细',
    },
    imgUrl: app.imgUrl,
  },
  onLoad(options) {
    app.pageOnLoad(this);
    let that = this;
    let title = options.title ? options.title : '提现明细';
    wx.setNavigationBarTitle({
      title: title,
    });
    let typeId = options.type_id ? Number(options.type_id) : null;
    that.setData({ typeId });
    app.loadCurrencyUnit().then(res => {
      that.setData({
        unit: res.unit,
      });
    });
    is_no_more = false;
    is_loading = false;
    p = 2;
    that.LoadCashList(options.status || -1);
  },
  //分销提现，商家提现或余额提现
  LoadCashList(sta) {
    let that = this;
    let status = parseInt(sta || -1, 0);
    that.setData({
      status,
    });
    wx.showLoading({
      title: '正在加载...',
      mask: true,
    });
    app.ws_micro_shoprequest({
      url: !that.data.typeId ? api.share.cash_detail : (Number(that.data.typeId) === 1 ? api.adminer.withdraw : api.user.withdraw),
      data: {
        status,
      },
      success: function (res) {
        wx.stopPullDownRefresh();
        if (res.code === 0) {
          that.setData({
            cash_list: res.data.list || [],
            cash_price: res.data.cash_price,
          });
        }
        
      },
      complete: function () {
        wx.hideLoading();
        that.setData({
          show_no_data_tip: true,
        });
      },
    });
  },
  switchlist(e) {
    let status = e.currentTarget.dataset.status;
    is_no_more = false;
    is_loading = false;
    p = 2;
    this.setData({
      is_show_line: false,
    })
    this.LoadCashList(status);
  },
  onPullDownRefresh() {
    this.LoadCashList(-1);
  },
  onReachBottom() {
    let that = this;
    if (is_loading || is_no_more) {
      return;
    }
    is_loading = true;
    wx.showLoading({
      title: '加载更多...',
      mask: true,
    });
    app.ws_micro_shoprequest({
      url: !that.data.typeId ? api.share.cash_detail : (Number(that.data.typeId) === 1 ? api.adminer.withdraw : api.user.withdraw),
      data: {
        status: that.data.status,
        page: p,
      },
      success: function (res) {
        if (res.code === 0) {
          let { list } = res.data;
          let cash_list = that.data.cash_list.concat(list);
          that.setData({
            cash_list,
          });
          wx.hideLoading();
          if (list.length === 0) {
            is_no_more = true;
            // wx.showToast({
            //   title: '没有更多数据了',
            //   icon: 'none',
            //   duration: 2000,
            // });
            that.setData({
              is_show_line: p > 1 ? true : false,
            })
          }
        }
        p++;
      },
      complete: function () {
        is_loading = false;
      },
    });
  },
  collectFormId(e) {
    app.collectFormId(e);
  },
});
